﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.TimesheetDataSetTableAdapters;
using Model;
using System.Data.Objects;

namespace Business
{
    public class TimesheetMngr
    {
        private static TimesheetTableAdapter tta = new TimesheetTableAdapter();
        private static TimesheetEntities context = new TimesheetEntities();

        public TimesheetMngr()
        {
            //if (tta == null)
                
        }

        /* Definition
         * returns the timesheet for the last 7 days for a specified employeeId 
         */
        public static IEnumerable<Timesheet> getTimesheetLast7Days(Employee e) {
/*
            var a = (from t in context.Timesheets
                     where t.Employee.id == e.id
                     select
                         new
                         {
                             SignInTimestamp = t.SignIn.timestamp,
                             SignOutTimestamp = t.SignOut.timestamp;// ?? DateTime.MinValue
                         }).ToList();
*/
            return (ObjectQuery<Timesheet>) context.Timesheets.Where(t => t.Employee.Equals(e));

        }

        /* Definition
         * returns the timesheet for the last 7 days for a specified employeeId 
         */
        public static Object getTimesheetLast7Days(int employeeId) {
            try
            {
                return tta.GetDataByEmployeeID(employeeId);
            }
            catch (InvalidOperationException ioe)
            {
                return null;
            } 
        }

        public static void sign(Model.Employee employee, string tipo, string ip)
        {
            Signature signature = new Signature();
            signature.timestamp = System.DateTime.Now;
            signature.ip = ip;
            signature.modified = false;

            Timesheet timesheet = null;

            switch (tipo){

                case "in" :
                    timesheet = context.CreateObject<Timesheet>();
                    timesheet.SignIn = signature;
                    timesheet.SignOut = null;
                    timesheet.Employee = employee;                    
                    // It saves the new timesheet into the database
                    context.AddToTimesheets(timesheet);
                    context.SaveChanges();
                   

                    break;                              

                case "out":
                    timesheet = context.Timesheets.First(t => t.employee_id == employee.id);
                    if (timesheet.SignOut == null)
                    {
                        timesheet.SignOut = signature;
                        // It saves the updated timesheet into the database
                        context.ApplyCurrentValues<Timesheet>("Timesheets", timesheet);
                        context.SaveChanges();
                        
                        return;
                    }
                    timesheet = context.CreateObject<Timesheet>();
                    timesheet.Employee = employee;
                    timesheet.SignOut = signature;
                    // It saves the new timesheet into the database
                    //context.AddToTimesheets(timesheet);
                    context.SaveChanges();
                    break;

            }
            
        }

    }
}
